package com.youku.plugins.accelerate
{
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.interfaces.IUserData;
   import com.youku.events.ConductorEvent;
   import com.youku.PlayerConfig;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.PluginEvent;
   import flash.display.MovieClip;
   
   public class AccelerateMediator extends Object
   {
      
      public function AccelerateMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._playerProxy = param2;
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetadataUpdated);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_START_TO_PLAY,this.onPlayerStartPlay);
         this._playerProxy.addEventListener(PluginEvent.ALERT_BOX_HIDE,this.onAlertBoxHide);
      }
      
      private var _playerProxy:IPlayerProxy;
      
      private var _userData:IUserData;
      
      private var _panelTimeout:Number = 2.592E9;
      
      private function onMetadataUpdated(param1:ConductorEvent) : void {
         this._userData = PlayerConfig.playListData.user;
         if(this._userData.acc_support == true)
         {
            if(this._userData.vip == false)
            {
               PlayerConfig.accState = 0;
            }
            if(this._userData.vip == true && this._userData.acc_open == false)
            {
               PlayerConfig.accState = 1;
            }
            if(this._userData.vip == true && this._userData.acc_open == true)
            {
               PlayerConfig.accState = 2;
            }
         }
      }
      
      private function onPlayerStartPlay(param1:PlayerControlEvent) : void {
         if(PlayerConfig.accState == 1 && this.checkFSOTime() == true)
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ALERT_BOX_SHOW,
               {
                  "type":"acc",
                  "from":"acc"
               }));
         }
         if(PlayerConfig.accState == 2)
         {
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ACCELERATE_INFO_SHOW));
         }
      }
      
      private function onAlertBoxHide(param1:PluginEvent) : void {
         var _loc2_:Date = null;
         var _loc3_:* = NaN;
         if((param1.data.from) && param1.data.from == "acc")
         {
            _loc2_ = new Date();
            _loc3_ = _loc2_.getTime();
            this._playerProxy.fsoProxy.saveAccTime(_loc3_);
         }
      }
      
      private function checkFSOTime() : Boolean {
         var _loc1_:Date = new Date();
         var _loc2_:Number = _loc1_.getTime();
         var _loc3_:Number = this._playerProxy.fsoProxy.getAccTime();
         return _loc2_ - _loc3_ > this._panelTimeout;
      }
   }
}
